Skip to content

Writing Classes

Anatomy of a Class

ENDURING UNDERSTANDING

LEARNING OBJECTIVE

Designate access and visibility constraints to classes, data, constructors, and methods.

ESSENTIAL KNOWLEDGE

  • The keywords public and private affect the access of classes, data, constructors, and methods.

  • The keyword private restricts access to the declaring class, while the keyword public allows access from classes outside the declaring class.

  • Classes are designated public. MOD-2.A.4 Access to attributes should be kept internal to the class. Therefore, instance variables are designated as private.

  • Constructors are designated public.

  • Access to behaviors can be internal or external to the class. Therefore, methods can be designated as either public or private.

ENDURING UNDERSTANDING

LEARNING OBJECTIVE

Designate private visibility of instance variables to encapsulate the attributes of an object.

ESSENTIAL KNOWLEDGE

  • Data encapsulation is a technique in which the implementation details of a class are kept hidden from the user.

  • When designing a class, programmers make decisions about what data to make accessible and modifiable from an external class. Data can be either accessible or modifiable, or it can be both or neither.

  • Instance variables are encapsulated by using the private access modifier.

  • The provided accessor and mutator methods in a class allow client code to use and modify data.

Constructors

ENDURING UNDERSTANDING

LEARNING OBJECTIVE

Define instance variables for the attributes to be initialized through the constructors of a class.

ESSENTIAL KNOWLEDGE

  • An object’s state refers to its attributes and their values at a given time and is defined by instance variables belonging to the object. This creates a “has-a” relationship between the object and its instance variables.

  • Constructors are used to set the initial state of an object, which should include initial values for all instance variables.

  • Constructor parameters are local variables to the constructor and provide data to initialize instance variables.

  • When a mutable object is a constructor parameter, the instance variable should be initialized with a copy of the referenced object. In this way, the instance variable is not an alias of the original object, and methods are prevented from modifying the state of the original object.

  • When no constructor is written, Java provides a no-argument constructor, and the instance variables are set to default values.

Documentation with Comments

ENDURING UNDERSTANDING

LEARNING OBJECTIVE

Describe the functionality and use of program code through comments.

ESSENTIAL KNOWLEDGE

  • Comments are ignored by the compiler and are not executed when the program is run.

  • Three types of comments in Java include /* _/, which generates a block of comments, //, which generates a comment on one line, and /** _/, which are Javadoc comments and are used to create API documentation.

  • A precondition is a condition that must be true just prior to the execution of a section of program code in order for the method to behave as expected. There is no expectation that the method will check to ensure preconditions are satisfied.

  • A postcondition is a condition that must always be true after the execution of a section of program code. Postconditions describe the outcome of the execution in terms of what is being returned or the state of an object.

  • Programmers write method code to satisfy the postconditions when preconditions are met.

Accessor Methods

ENDURING UNDERSTANDING

LEARNING OBJECTIVE

Define behaviors of an object through non-void methods without parameters written in a class.

ESSENTIAL KNOWLEDGE

  • An accessor method allows other objects to obtain the value of instance variables or static variables.

  • A non-void method returns a single value. Its header includes the return type in place of the keyword void.

  • In non-void methods, a return expression compatible with the return type is evaluated, and a copy of that value is returned. This is referred to as “return by value.”

  • When the return expression is a reference to an object, a copy of that reference is returned, not a copy of the object.

  • The return keyword is used to return the flow of control to the point immediately following where the method or constructor was called.

  • The toString method is an overridden method that is included in classes to provide a description of a specific object. It generally includes what values are stored in the instance data of the object.

  • If System.out.print or System.out. println is passed an object, that object’s toString method is called, and the returned string is printed.

Mutator Methods

ENDURING UNDERSTANDING

LEARNING OBJECTIVE

Define behaviors of an object through void methods with or without parameters written in a class.

ESSENTIAL KNOWLEDGE

  • A void method does not return a value. Its header contains the keyword void before the method name.

  • A mutator (modifier) method is often a void method that changes the values of instance variables or static variables.

Writing Methods

ENDURING UNDERSTANDING

LEARNING OBJECTIVE

Define behaviors of an object through non-void methods with parameters written in a class.

ESSENTIAL KNOWLEDGE

  • Methods can only access the private data and methods of a parameter that is a reference to an object when the parameter is the same type as the method’s enclosing class.

  • Non-void methods with parameters receive values through parameters, use those values, and return a computed value of the specified type.

  • It is good programming practice to not modify mutable objects that are passed as parameters unless required in the specification.

  • When an actual parameter is a primitive value, the formal parameter is initialized with a copy of that value. Changes to the formal parameter have no effect on the corresponding actual parameter.

  • When an actual parameter is a reference to an object, the formal parameter is initialized with a copy of that reference, not a copy of the object. If the reference is to a mutable object, the method or constructor can use this reference to alter the state of the object.

  • Passing a reference parameter results in the formal parameter and the actual parameter being aliases. They both refer to the same object.

Static Variable and Methods

ENDURING UNDERSTANDING

LEARNING OBJECTIVE

Define behaviors of a class through static methods.

ESSENTIAL KNOWLEDGE

  • Static methods are associated with the class, not objects of the class.

  • Static methods include the keyword static in the header before the method name.

  • Static methods cannot access or change the values of instance variables.

  • Static methods can access or change the values of static variables.

  • Static methods do not have a this reference and are unable to use the class’s instance variables or call non-static methods.

ENDURING UNDERSTANDING

LEARNING OBJECTIVE

Define the static variables that belong to the class.

ESSENTIAL KNOWLEDGE

  • Static variables belong to the class, with all objects of a class sharing a single static variable.

  • Static variables can be designated as either public or private and are designated with the static keyword before the variable type.

  • Static variables are used with the class name and the dot operator, since they are associated with a class, not objects of a class.

Scope and Access

ENDURING UNDERSTANDING

LEARNING OBJECTIVE

Explain where variables can be used in the program code.

ESSENTIAL KNOWLEDGE

  • Local variables can be declared in the body of constructors and methods. These variables may only be used within the constructor or method and cannot be declared to be public or private.

  • When there is a local variable with the same name as an instance variable, the variable name will refer to the local variable instead of the instance variable.

  • Formal parameters and variables declared in a method or constructor can only be used within that method or constructor.

  • Through method decomposition, a programmer breaks down a large problem into smaller subproblems by creating methods to solve each individual subproblem.

this Keyword

ENDURING UNDERSTANDING

LEARNING OBJECTIVE

Evaluate object reference expressions that use the keyword this.

ESSENTIAL KNOWLEDGE

  • Within a non-static method or a constructor, the keyword this is a reference to the current object—the object whose method or constructor is being called.

  • The keyword this can be used to pass the current object as an actual parameter in a method call.

Ethical and Social Implications of Computing Systems

ENDURING UNDERSTANDING

LEARNING OBJECTIVE

Explain the ethical and social implications of computing systems.

ESSENTIAL KNOWLEDGE

  • System reliability is limited. Programmers should make an effort to maximize system reliability.

  • Legal issues and intellectual property concerns arise when creating programs.

  • The creation of programs has impacts on society, economies, and culture. These impacts can be beneficial and/or harmful.